package com.xyoto.community.service.impl;

import com.xyoto.community.constant.*;
import com.xyoto.community.dto.SaveQuestionCommentLoveDTO;
import com.xyoto.community.entity.comment.QuestionComment;
import com.xyoto.community.entity.love.QuestionCommentLove;
import com.xyoto.community.entity.main.Question;
import com.xyoto.community.enums.LoveEnum;
import com.xyoto.community.enums.QuestionsEnum;
import com.xyoto.community.enums.WechatLoginEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.comment.QuestionCommentMapper;
import com.xyoto.community.mapper.love.QuestionCommentLoveMapper;
import com.xyoto.community.mapper.main.MessageMapper;
import com.xyoto.community.mapper.main.QuestionMapper;
import com.xyoto.community.service.MessageService;
import com.xyoto.community.service.QuestionCommentLoveService;
import com.xyoto.community.utils.JwtUtils;
import com.xyoto.community.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassName:   问答父级评论点赞service接口实现类
 * Author:      挽风
 * Date:        2020
 * Copyright:   2020 by 挽风1.0版本
 * Description:
 **/
//@Service
@Slf4j
public class QuestionCommentLoveServiceImpl implements QuestionCommentLoveService {

    @Autowired
    private QuestionCommentLoveMapper questionCommentLoveMapper;

    @Autowired
    private QuestionCommentLoveMapper commentLoveMapper;

    @Autowired
    private HttpServletRequest request;

    @Resource
    private QuestionCommentMapper questionCommentMapper;

    @Resource
    private QuestionMapper questionMapper;

    @Autowired
    private MessageService messageService;

    @Resource
    private MessageMapper messageMapper;

    /**
     * 根据问答评论id查询父级评论点赞数
     *
     * @param id 问答id
     *
     * @return 点赞数
     */
    @Override
    public Integer getQuestionCommentsLoveCountById(Integer id) {
        return questionCommentLoveMapper.getQuestionCommentLoveCountById(id);
    }

    /**
     * 新增问答评论点赞
     * @param commentLoveDTO
     * @return
     */
    @Override
    public Result saveQuestionCommentLove(SaveQuestionCommentLoveDTO commentLoveDTO) {

        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        if (userId == UserLoginStatus.UN_LOGIN) {ExceptionUtils.error(WechatLoginEnum.USER_NOT_LOGIN);}

        //判断是否有该评论
        QuestionComment getQuestionComment = questionCommentMapper.getQuestionCommentById(commentLoveDTO.getQuestionCommentId());
        if (getQuestionComment == null || getQuestionComment.getDeleteStatus() == DeleteStatus.YES) {
            ExceptionUtils.error(QuestionsEnum.QUESTION_COMMENTS_NOT_EXIST);
        }

        //判断问答是否存在
        Question questionById = questionMapper.getQuestionById(getQuestionComment.getQuestionId());
        if (questionById == null || questionById.getDeleteStatus() == DeleteStatus.YES){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_NULL);
        }

        //判断是否已经点过赞了
        QuestionCommentLove loveStatus = questionCommentLoveMapper.getQuestionCommentLoveStatus(userId, commentLoveDTO.getQuestionCommentId());
        if (loveStatus != null && loveStatus.getStatus() == LoveStatus.LOVE){
            ExceptionUtils.error(LoveEnum.QUESTION_COMMENT_LOVED);
        }

        log.info("开始执行新增问答评论点赞功能");
        //若无上述条件则执行新增问答评论点赞功能
        QuestionCommentLove commentLove = new QuestionCommentLove();
        commentLove.setQuestionCommentId(commentLoveDTO.getQuestionCommentId());
        commentLove.setStatus(QuestionCommentLoveStatus.LOVE);
        commentLove.setUserId(userId);

        log.info("开始执行SQL");
        Integer saveCommentLove = commentLoveMapper.saveCommentLove(commentLove);
        if (saveCommentLove == null || saveCommentLove == 0){
            ExceptionUtils.error(LoveEnum.QUESTION_COMMENT_LOVE_FAIL);
        }

//        //开始存入消息
//        messageService.saveMessage(userId,questionById.getUserId(),commentLove.getId(),
//                ModuleType.QUESTION_COMMENT_LOVE,questionById.getId());

        return Result.success();
    }

    /**
     * 取消问答评论点赞
     * @param questionCommentId 问答评论id
     * @return
     */
    @Override
    @Transactional
    public Result removeQuestionCommentLove(Integer questionCommentId){
        Integer userId = JwtUtils.getMemberIdByJwtToken(request);
        //非空校验
        if (null == questionCommentId || questionCommentId <= 0){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_ID_ERROR);
        }
        QuestionComment questionComment = questionCommentMapper
                .getQuestionCommentById(questionCommentId);
        //校验问答评论
        checkQuestionComment(questionComment);
        Question question = questionMapper.getQuestionById(questionComment.getQuestionId());
        //检验问答
        checkQuestion(question);
        //检验点赞状态
        QuestionCommentLove commentLove = questionCommentLoveMapper
                .getQuestionCommentLove(userId, questionCommentId);
        if (commentLove.getStatus().equals(DeleteStatus.NO)){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_LOVE_IS_EXIST);
        }
        //业务
        QuestionCommentLove questionCommentLove = new QuestionCommentLove();
        questionCommentLove.setQuestionCommentId(questionCommentId);
        questionCommentLove.setUserId(userId);
        questionCommentLove.setStatus(QuestionCommentLoveStatus.DELETE_LOVE);
        Integer state = questionCommentLoveMapper
                .removeQuestionCommentLove(questionCommentLove);
        if (state != OperationStatus.SUCCESS){
            ExceptionUtils.error(LoveEnum.QUESTIONS_COMMENT_LOVE_FAILED);
        }
//        //删除点赞消息
//        Message message = new Message();
//        message.setType(ModuleType.QUESTION_COMMENT_LOVE);
//        message.setModuleId(commentLove.getId());
//        message.setThirdParentId(question.getId());
//        message.setUserId(userId);
//        Integer status = messageMapper
//                .removeMessage(message);
//        if (status != OperationStatus.SUCCESS){
//            ExceptionUtils.error(MessageEnum.DELETED_QUESTION_COMMENT_REPLY_LOVE_MESSAGE_FAILED);
//        }
        return Result.success();
    }

    /**
     * 校验问答评论是否存在
     * @param questionComment
     */
    private void checkQuestionComment(QuestionComment questionComment){
        if (null == questionComment){
            ExceptionUtils.error(QuestionsEnum.QUESTION_COMMENTS_NOT_EXIST);
        }
        //检验问答评论是否删除
        if (questionComment.getDeleteStatus().equals(DeleteStatus.YES)){
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_COMMENT_IS_DELETE);
        }
    }

    /**
     * 校验问答是否存在
     * @param question
     */
    private void checkQuestion(Question question) {
        if (null == question) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_NULL);
        }
        //检验问答是否删除
        if (question.getDeleteStatus().equals(DeleteStatus.YES)) {
            ExceptionUtils.error(QuestionsEnum.QUESTIONS_IS_DELETE);
        }
    }

}
